/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.tag;

import java.util.List;

import javax.servlet.jsp.tagext.BodyTagSupport;

import cn.qidisoft.edu.hzjt.model.Priority;

/**
 * 按钮权限控制
 * 
 * @author hxq
 * @version $Id: PermissionTag.java, v 0.1 2018年10月27日 上午10:28:07 hxq Exp $
 */
public class PermissionTag extends BodyTagSupport {

  /**  */
  private static final long serialVersionUID = 1L;
  /** 用户拥有所有权限 **/
  private List<Priority> permisstionList;
  /** 权限key **/
  private String key;
  /** 当前用户操作栏目ID **/
  private String currentCatalogId;
  /** 是否是管理员 **/
  private Integer isAdmin;
  /** 提交权限 **/
  private String submitPermission;
  /** 审核权限 **/
  private String checkPermission;
  /** 其他权限 **/
  private String otherPermission;
  /** 收回的权限 **/
  private String cancelRevokePermission;
  private Object currentUser;
  /** 管理权限 **/
  private final static String Key_Manager = "manager";
  /** 新增权限 **/
  private final static String Key_Add = "add";
  /** 修改权限 **/
  private final static String Key_Modify = "modify";
  /** 删除权限 **/
  private final static String Key_Delete = "delete";
  /** 提交权限 **/
  private final static String Key_Submit = "submit";
  /** 审核权限 **/
  private final static String Key_Check = "check";
  /** 其他权限 **/
  private final static String Key_Other = "other";

  public int doStartTag() {
    if (isPermisstion()) {// 如果拥有这个权限 则输出
      return EVAL_BODY_INCLUDE;
    }
    return SKIP_BODY;
  }

  public boolean isPermisstion() {
    boolean isPermission = false;
    // 是否是管理员
    // if (isAdmin != null && isAdmin == Constant.IS_ADMIN) {
    // return isPermission;
    // }
    // 取消权限
    if (cancelRevokePermission != null && cancelRevokePermission.trim().equals("true")) {
      return isPermission;
    }
    isPermission = isByPermisstionList();
    return isPermission;
  }

  public boolean isByPermisstionList() {
    boolean isPermission = false;
    if (permisstionList == null) {
      return isPermission;
    }
    for (Priority p : permisstionList) {
      if (p.getCellid().equals(currentCatalogId)) {
        isPermission = isPermissionCommon(p); // 正常栏目操作
        if (isPermission) {// 如果已经拥有这个权限，则不在需要循环权限了
          break;
        }
      }
    }
    return isPermission;
  }

  private boolean isPermissionCommon(Priority priority) {
    if (key.equals(Key_Manager) && priority.getIsadmin() == true) {// 是否有其他操作权限
      return true;
    }
    if (key.equals(Key_Add) && (priority.getIsadmin() == true || priority.getCancreate() == true)) {// 是否有新增
      return true;
    }
    if (key.equals(Key_Modify)
        && (priority.getIsadmin() == true || priority.getCanmodify() == true)) {// 是否有编辑 更新
      return true;
    }
    if (key.equals(Key_Delete)
        && (priority.getIsadmin() == true || priority.getCandelete() == true)) {// 是否有删除
      return true;
    }
    if (key.equals(Key_Submit)
        && (priority.getIsadmin() == true || submitPermission.equals("true"))) {// 是否有提交权限
      return true;
    }
    if (key.equals(Key_Check)
        && (priority.getIsadmin() == true || checkPermission.equals("true"))) {// 是否有审核权限
      return true;
    }
    if (key.equals(Key_Other)
        && (priority.getIsadmin() == true || otherPermission.equals("true"))) {// 是否有审核权限
      return true;
    }

    return false;
  }

  public List<Priority> getPermisstionList() {
    return permisstionList;
  }

  public void setPermisstionList(List<Priority> permisstionList) {
    this.permisstionList = permisstionList;
  }

  public String getKey() {
    return key;
  }

  public void setKey(String key) {
    this.key = key;
  }

  public String getCurrentCatalogId() {
    return currentCatalogId;
  }

  public void setCurrentCatalogId(String currentCatalogId) {
    this.currentCatalogId = currentCatalogId;
  }


  public Object getCurrentUser() {
    return currentUser;
  }

  public void setCurrentUser(Object currentUser) {
    this.currentUser = currentUser;
  }

  public Integer getIsAdmin() {
    return isAdmin;
  }

  public void setIsAdmin(Integer isAdmin) {
    this.isAdmin = isAdmin;
  }

  public String getSubmitPermission() {
    return submitPermission;
  }

  public void setSubmitPermission(String submitPermission) {
    this.submitPermission = submitPermission;
  }

  public String getCheckPermission() {
    return checkPermission;
  }

  public void setCheckPermission(String checkPermission) {
    this.checkPermission = checkPermission;
  }

  public String getOtherPermission() {
    return otherPermission;
  }

  public void setOtherPermission(String otherPermission) {
    this.otherPermission = otherPermission;
  }

  public String getCancelRevokePermission() {
    return cancelRevokePermission;
  }

  public void setCancelRevokePermission(String cancelRevokePermission) {
    this.cancelRevokePermission = cancelRevokePermission;
  }

}


